/**
Engine.hpp
Title: Binge Engine Header File
Descr: Engine Class

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#ifndef BINGE_ENGINE_HPP
#define BINGE_ENGINE_HPP

#include <unistd.h>
#include <ctime>
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../../binge/BaseGame.hpp"
#include "../../binge/core/Audio.hpp"
#include "../../binge/core/Dialog.hpp"
#include "../../binge/core/Timer.hpp"
#include "../../binge/objects/Entity.hpp"
#include "../../binge/utilities/Vector3.hpp"

#define VERSION_MAJOR 0
#define VERSION_MINOR 2

namespace binge
{

class Engine
{
public:
	Engine(void);
	virtual ~Engine(void);

	bool gameover;
    sf::RenderWindow screen;
	Audio* audio;

	bool Init(void);

	void Close(	void (BaseGame::*game_end)(void),
				BaseGame* basegame);
	void Update(void (BaseGame::*game_update)(void),
				void (BaseGame::*game_render)(void),
				BaseGame* basegame);

	void Message(std::string message, std::string title, int icon = wxICON_EXCLAMATION);
	void Fatal_Error(std::string message, std::string title);
	void Shutdown(void);
	bool Render_Start(void);
	bool Render_Stop(void);
	int Release(void);

	//accessor/mutator functions expose the private variables
	bool isMouseVisible(void) { return mouseVisible; }
	void setMouseVisible(bool visible) { mouseVisible = visible; screen.ShowMouseCursor(mouseVisible); }
	GameMode getGameMode(void) { return gameMode; }
	void setGameMode(enum GameMode mode) { gameMode = mode; }
	std::string getGameTitle(void) { return gameTitle; }
	void setGameTitle(std::string title) { gameTitle = title; }
	int getVersionMajor(void) { return versionMajor; }
	int getVersionMinor(void) { return versionMinor; }
	std::string getVersionText(void);
	long getFrameRate_core(void) { return frameRateCore; };
	long getFrameRate_real(void) { return frameRateReal; };
	int getScreenWidth(void) { return screenWidth; }
	void setScreenWidth(int value) { screenWidth = value; }
	int getScreenHeight(void) { return screenHeight; }
	void setScreenHeight(int value) { screenHeight = value; }
	int getColorDepth(void) { return colorDepth; }
	void setColorDepth(int value) { colorDepth = value; }
	bool getFullscreen(void) { return fullscreen; }
	void setFullscreen(bool value) { fullscreen = value; }
	bool getMaximizeProcessor(void) { return maximizeProcessor; }
	void setMaximizeProcessor(bool value) { maximizeProcessor = value; }
	int getEngineFrameRateMS(void) { return engineFrameRateMS; }
	void setEngineFrameRateMS(int ms) { engineFrameRateMS = ms; }

	std::list<Entity*> getEntityList(void) { return entityList; }
	long getEntityCount() { return (long)entityList.size(); }
	void addEntity(Entity *entity);
	Entity* FindEntity(std::string name);
	Entity* FindEntity(int objectType);

private:
	int versionMajor, versionMinor;
	std::string gameTitle;
	bool fullscreen;
	int screenWidth;
	int screenHeight;
	int colorDepth;
	bool mouseVisible;
	enum GameMode gameMode;
	bool maximizeProcessor;
	long frameCountCore;
	long frameRateCore;
	long frameCountReal;
	long frameRateReal;
	int engineFrameRateMS;
    Dialog dialog;


	// Engine Timers:
	Timer timedUpdate;
	Timer coreTimer;
	Timer realTimer;
	Timer collisionTimer;


	bool Init_SFML(void);
	bool Init_OpenGL(void);

	std::list<Entity*> entityList;
	void UpdateEntities(	void (BaseGame::*game_entity_update)(Entity* entity),
							BaseGame* basegame);
	void DrawEntities(		void (BaseGame::*game_entity_render)(Entity* entity),
							BaseGame* basegame);
	void BuryEntities(void);
	int getEntityCount(int objectType);

	bool Collision(Entity* entity1, Entity* entity2);
	bool CollisionBR(Entity* entity1, Entity* entity2);
	bool CollisionD(Entity* entity1, Entity* entity2);
	void TestForCollisions(	void (BaseGame::*game_entity_collision)(Entity* entity1, Entity* entity2),
							BaseGame* basegame);
};

} //namespace

#endif
